package cn.example.collection;

import cn.example.entity.People;
import com.alibaba.fastjson2.JSON;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author July
 * @description 求两个map集合的交集
 * @create 2025-11-19 21:42
 */
public class MapValueIntersection {

    public static void main(String[] args) {
        Map<Integer, People> map = new HashMap<>();
        People p1 = new People("张三", List.of(1, 2, 3));
        People p2 = new People("李四", List.of(3, 4, 5));
        People p3 = new People("王五", List.of(6, 7, 8));
        map.put(1, p1);
        map.put(2, p2);
        Map<String, People> map2 = new HashMap<>();
        map2.put("p1", p1);
        map2.put("p2", p3);

        Set<People> value1 = new HashSet<>(map.values());
        Set<People> value2 = new HashSet<>(map2.values());

        Set<People> collect = value1.stream().filter(value2::contains).collect(Collectors.toSet());
        System.out.println(JSON.toJSONString( collect));

        Set<People> collect1 = value1.stream().filter(v1 -> value2.stream().anyMatch(v2 -> v1.getName().equals(v2.getName())))
                .collect(Collectors.toSet());
        System.out.println(JSON.toJSONString( collect1));
    }
}
